package LeetCode;

public class Interview_02_04 {
    public ListNode partition(ListNode head, int x) {
        ListNode smallHead = new ListNode(101);
        ListNode bigHead = new ListNode(101);
        ListNode smallTail = smallHead;
        ListNode bigTail = bigHead;
//        判定边界
        if (head == null) {
            return head;
        }
        while (head != null) {
            if (head.val < x) {
//                尾插进smallHead
                smallTail.next = head;
                smallTail = head;
            }else {
//                尾插进bigHead
                bigTail.next = head;
                bigTail = head;
            }
            head = head.next;
        }
//        将smallHead与bigHead合并起来
        bigTail.next = null;
        smallTail.next = bigHead.next;
        return smallHead.next;
    }
}
